package com.example.forumsystem.demos.web.service.impl;

import com.example.forumsystem.demos.web.common.ResultCode;
import com.example.forumsystem.demos.web.exception.ApplicationException;
import com.example.forumsystem.demos.web.mapper.BoardInfoMapper;
import com.example.forumsystem.demos.web.mapper.UserInfoMapper;
import com.example.forumsystem.demos.web.model.BoardInfo;
import com.example.forumsystem.demos.web.model.Result;
import com.example.forumsystem.demos.web.model.UserInfo;
import com.example.forumsystem.demos.web.service.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class BoardServiceImpl implements IBoardService {
    @Autowired
    private BoardInfoMapper boardInfoMapper;

    @Override
    public List<BoardInfo> selectBoardByNum(Integer num) {
        if (num == null){
            log.info("未传来需要限制的导航栏个数, num :" + num);
            throw new ApplicationException(new Result("未传来需要限制的导航栏个数"));
        }

        List<BoardInfo> boardInfos = boardInfoMapper.selectBoardByNum(num);
        return boardInfos;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        if (id == null || id <= 0) {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        BoardInfo board = boardInfoMapper.selectByPrimaryKey(id);
        if (board == null) {
            log.warn(ResultCode.ERROR_IS_NULL.toString() + ", board id = " + id);
            throw new ApplicationException(Result.fail(ResultCode.ERROR_IS_NULL));
        }

        BoardInfo updateBoard = new BoardInfo();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() + 1);

        int row = boardInfoMapper.updateByPrimaryKeySelective(updateBoard);

        if (row != 1) {
            log.warn(ResultCode.FAILED.toString() + ", 受影响的行数不等于 1 .");
            throw new ApplicationException(Result.fail(ResultCode.FAILED));
        }
    }

    @Override
    public BoardInfo selectById(Long id) {
        if (id == null || id <= 0) {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        BoardInfo board = boardInfoMapper.selectByPrimaryKey(id);
        return board;
    }

    @Override
    public void cutOneArticleCountById(Long boardId) {
        if (boardId == null || boardId <= 0) {
            log.warn("传入的板块Id有误，为: " + boardId);
            throw new ApplicationException(Result.fail(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        BoardInfo board = boardInfoMapper.selectByPrimaryKey(boardId);
        if (board == null) {
            log.warn("该板块不存在，板块id为：" + boardId);
            throw new ApplicationException(Result.fail(ResultCode.ERROR_IS_NULL));
        }

        BoardInfo updateBoard = new BoardInfo();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() - 1);

        int row = boardInfoMapper.updateByPrimaryKeySelective(updateBoard);
        if (row != 1) {
            log.warn("更新板块帖子数失败");
            throw new ApplicationException(Result.fail(ResultCode.FAILED));
        }
    }
}
